package com.ustc.hewei.snowflakemodule.snowFlake;

import com.ustc.hewei.snowflakemodule.SnowFlakemoduleApplication;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.TimeUnit;

/**
* @description: 雪花算法测试类
* @author hewei
* @date 2022/9/23 21:11
* @version 1.0
*/

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
@Threads(5)
public class BenchmarkSnowflake {

    private SnowflakeImpl test1;

    @org.openjdk.jmh.annotations.Benchmark
    @CompilerControl(CompilerControl.Mode.DONT_INLINE)
    public Long test() {
        Long temp = test1.getSnowflakeId("127.0.0.1", "6666");
        System.out.println(temp);
        return temp;
    }

    public static void main(String[] args) throws Exception {
        // 使用一个单独进程执行测试，执行5遍warmup，然后执行3遍测试
        Options opt = new OptionsBuilder().include(BenchmarkSnowflake.class.getSimpleName()).forks(1).warmupIterations(5)
                .measurementIterations(3).output("/Users/hewei/java-project/myMiaoshaWithRpc/snowFlakemodule/src/main/resources/Benchmark-snowflake-mutilThreads.json").build();
        new Runner(opt).run();
    }

    @Setup
    public void init() {
        ConfigurableApplicationContext context = SpringApplication.run(SnowFlakemoduleApplication.class);
        test1 = context.getBean(SnowflakeImpl.class);
    }
}
